<?php
declare (strict_types = 1);

namespace app\company\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Session;


class Index extends BaseController
{
    public function index()
    {
        return '您好！这是一个[company]示例应用';
    }
    public function add(){
        if(request()->isPost()){
            $data=input('post.');
            // if($_FILES['image']['tmp_name']){
            //     $data['image']=$this->upload();
            // }
            $add=Db::table('company')->insert($data);
            if($add){
                Session::set('message','发布成功');
                return redirect('index');
            }else{
                Session::set('message','发布失败');
                return redirect('index');
            }          
            return;
        }
        return view();
    }

    public function del($id){
        $del=Db::table('company')->where('id',$id)->delete();
        if($del){
            session::set('message','删除成功');
            return redirect('index');
        }else{
            Session::set('message','删除失败');
            return redirect('list'); 
        }       
    }

    public function edit(){        
            if(request()->isPost()){
               $data=input('post.');   
   
               if($data['company_url']&&stripos($data['company_url'],'http://')===false){
                   $data['company_url'] = 'http://'.$data['company_url'];   
               }
               $edit=Db::table('company')->where('id',$data['id'])->update($data);
               if($edit){
                Session::set('message','修改成功');
                return redirect('list');
           }
        }
           $id = input('id');
           $company = Db::table('company')->find($id);
             View::assign('company',$company);
           return view();
       }      

    public function list(){
        $companyRes=Db::table('company')->paginate(6);
        View::assign('companyRes',$companyRes);
        return view();
    }

    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
    }
    
}
